// Code from Visualizing Data, First Edition, Copyright 2008 Ben Fry.


class WordItem extends SimpleMapItem {
  String word;
  
  float boxLeft, boxTop;
  float boxRight, boxBottom;

  WordItem(String word) {
    this.word = word;
  }

  void draw() {
    stroke(0);
    strokeWeight(1.5);
    fill(100, 125);
    rect(x, y, w, h);
    calcBox();

    fill(0);
    if (w > textWidth(word) + 6) {
      if (h > textAscent() + 6) {
        textAlign(CENTER, CENTER);
        text(word, x + w/2, y + h/2);
      }
    }
    if (mouseInside()) {
      rolloverItem = this;
      //println(word);
    }
  }
  

  void calcBox() {
    boxLeft = zoomBounds.spanX(x, 0, mapWidth);
    boxRight = zoomBounds.spanX(x+w, 0, mapWidth);
    boxTop = zoomBounds.spanY(y+mapGraphCornerY, 0, mapHeight);
    boxBottom = zoomBounds.spanY(y+h+mapGraphCornerY, 0, mapHeight);
  }
  
  boolean mouseInside() {
    return (mouseX > boxLeft && mouseX < boxRight && 
            mouseY > boxTop && mouseY < boxBottom);    
  }
  
  String getWord() {
    return word;
  }

}
